package main

import (
	"encoding/json"
	"fmt"
	"strconv"
)

type PKresult struct {
	Studentid int
	Pkid      int
	Result    []qresult
	PKDate    string
}

type PKresult2 struct {
	Studentid string
}

type qresult struct {
	Question_id int
	Is_right    int
}

func main() {
	resultjson := "{\"studentid\":\"1922230\",\"pkid\":\"6187323\",\"pkdate\":\"2017-08-30 00:00:19\",\"result\":[{\"question_id\":269964,\"is_right\":1},{\"question_id\":449,\"is_right\":1},{\"question_id\":272611,\"is_right\":1},{\"question_id\":147654,\"is_right\":1},{\"question_id\":325110,\"is_right\":1},{\"question_id\":318772,\"is_right\":1},{\"question_id\":287694,\"is_right\":1},{\"question_id\":371350,\"is_right\":1},{\"question_id\":243014,\"is_right\":1},{\"question_id\":276257,\"is_right\":1},{\"question_id\":273178,\"is_right\":1},{\"question_id\":233408,\"is_right\":1},{\"question_id\":175574,\"is_right\":0},{\"question_id\":147659,\"is_right\":1},{\"question_id\":273241,\"is_right\":1},{\"question_id\":3424,\"is_right\":1},{\"question_id\":13606,\"is_right\":1},{\"question_id\":163945,\"is_right\":1},{\"question_id\":188192,\"is_right\":0},{\"question_id\":980,\"is_right\":0},{\"question_id\":290283,\"is_right\":1},{\"question_id\":213365,\"is_right\":1},{\"question_id\":233415,\"is_right\":1},{\"question_id\":277662,\"is_right\":1}]}"
	studentid, pkDate, memoresult, wrongquestion, rightquestion := readJson(resultjson)
	fmt.Println(studentid, pkDate, memoresult, wrongquestion, rightquestion)
	fmt.Println("222222222")
	var s2 PKresult2
	json.Unmarshal([]byte(resultjson), &s2)
	id, _ := strconv.Atoi(s2.Studentid)
	fmt.Println(s2, s2.Studentid, id)
}

//从json中得到需要更新的信息
func readJson(str string) (int, string, map[int]map[int]int, []int, []int) {
	var s PKresult
	memoRight := make(map[int]map[int]int)
	var wrongquestion []int
	var rightquestion []int
	//{"studentid":1234,"pkid":456,"result":[{"question_id":123456,"is_right":1},{"question_id":1234356,"is_right":1}]}
	json.Unmarshal([]byte(str), &s)
	for _, qresulti := range s.Result {
		if qresulti.Is_right == 1 {
			rightquestion = append(rightquestion, qresulti.Question_id)
		} else {
			wrongquestion = append(wrongquestion, qresulti.Question_id)
		}

		memo := 1
		word := 0
		if _, ok := memoRight[word]; ok {
			temp := memoRight[word]
			if _, ok := temp[memo]; !ok {
				temp[memo] = qresulti.Is_right
			} else {
				if qresulti.Is_right == 0 {
					temp[memo] = qresulti.Is_right
				}
			}
		} else {
			temp := make(map[int]int)
			temp[memo] = qresulti.Is_right
			memoRight[word] = temp
		}
	}
	return s.Studentid, s.PKDate, memoRight, wrongquestion, rightquestion
}
